package org.springblade.common.utils;

import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.springblade.common.bean.DynamicBean;

import java.beans.PropertyDescriptor;
import java.util.Map;

public class BeanAddPropertiesUtil {
	public static Object getTarget(Object dest, Map<String, Object> addProperties) {
		// get property map
		PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
		PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
		Map<String, Class> propertyMap = Maps.newHashMap();
		for (PropertyDescriptor d : descriptors) {
			if (!"class".equalsIgnoreCase(d.getName())) {
				propertyMap.put(d.getName(), d.getPropertyType());
			}
		}
		// add extra properties
		addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
		// new dynamic bean
		DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
		// add old value
		propertyMap.forEach((k, v) -> {
			try {
				// filter extra properties
				if (!addProperties.containsKey(k)) {
					dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		// add extra value
		addProperties.forEach((k, v) -> {
			try {
				dynamicBean.setValue(k, v);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		Object target = dynamicBean.getTarget();
		return target;

	}
}
